<template>
	<view class="leave_box">
    <navTitle :titleRight="'報告記錄'" :title="'上傳工作內容'" :jumpUrl="jumpUrl"></navTitle>
    <view class="rb-title">匯報類型</view>
    <view class="rb-tabs">
      <view class="rb-t-item" v-for="(item,index) in types" :class="typeIndex==index?'active':''" :key="index" @tap="handleChooseType(item,index)">{{ item }}</view>
    </view>
    <view class="rb-title">工作日期</view>
    <view class="rb-rq">
      <uni-datetime-picker type="daterange" v-model="chooseDate" placeholder="請選擇工作日期" @change="changeCalendar" />
    </view>
    <view class="rb-title">工作類型</view>
    <view class="rb-lx" @tap="handleLX()">
      <view class="lx-l">{{ lxList[lxIndex] }}</view>
      <img class="close" src="/static/images/boss/icon_b_arrow_down.png" mode="scaleToFill" alt="">
    </view>
    <view class="rb-title">工作內容</view>
    <view class="rb-block">
      <textarea class="reason-content" v-model="content" maxlength="120" placeholder="請輸入工作內容" placeholder-class="input--pl" fixed></textarea>
    </view>
    <view class="rb-imgs">
      <view class="rb-tp-p" @tap="uploadImages()">
        <view>現場圖片</view>
        <img src="/static/images/boss/icon_b_arrow_add_img.png" mode="scaleToFill" alt="">
      </view>
      <view class="xc-imgs" v-if="upload_list.length>0">
        <view class="xc-img" v-for="(item,index) in upload_list" :key="index">
          <img class="close" src="/static/images/boss/icon_xc_close.png" mode="scaleToFill" alt="">
          <img :src="item" mode="scaleToFill" alt="">
        </view>
      </view>
    </view>
    <view class="rb-imgs">
      <view class="rb-tp-p" @tap="uploadFiles()">
        <view>上傳附件</view>
        <img src="/static/images/boss/icon_b_arrow_add_img.png" mode="scaleToFill" alt="">
      </view>
      <view class="xc-files" v-if="file_list.length>0">
        <view class="xc-file" v-for="(item,index) in file_list" :key="index">
          <img src="/static/images/boss/icon_xc_file.png" mode="scaleToFill" alt="">
          <view class="xc-file-name">文件{{ parseInt(index+1) }}.{{ item|filterType() }}</view>
          <img class="img-point" src="/static/images/boss/icon_xc_point.png" mode="scaleToFill" alt="">
        </view>
      </view>
    </view>
    <view class="block-sign">
      <view class="p-sign">
        <view>管理人員姓名及簽署</view>
        <img src="/static/images/boss/icon_b_arrow_down.png" mode="scaleToFill" alt="">
      </view>
      <view class="box-sign">
        <img v-if="signImgUrl" :src="signImgUrl" mode="scaleToFill" alt="">
        <p v-else @tap="handleSign()">去簽署</p>
      </view>
    </view>
    <view class="btn-submit" @tap="handleSubmit()">提交</view>
	</view>
</template>

<script>
import { askleaveType,attendanceUploadwork } from '../../utils/api';
import navTitle from './components/nav-title.vue';
export default {
	components: {navTitle},
	data() {
		return {
      chooseDate:[],
      begin:'',
      end:'',
      typeIndex:0,
      types:['日報','周報','月報'],
      lxIndex:0,
      lxList:['代工','拆改','後加'],
      content:'',
      upload_list:[],
      file_list:[],
      signImgUrl:'',
      jumpUrl:'/pages/boss/reportRecord',
		};
	},
	props: {
	},
	computed: {},
  onShow() {
    let currentDate=this.$moment().format('YYYY-MM-DD')
    this.signImgUrl=uni.getStorageSync('signImgUrl')
  },
  filters:{
    filterType(val){
      let list=val.split('.')
      let str=list[list.length-1]
      return str
    }
  },
	methods: {
    handleChooseType(item,index){
      this.typeIndex=index
    },
    handleSign(){
      uni.navigateTo({ url: '/pages/sign/sign' })
    },
    uploadFiles(){
      let that=this
      uni.chooseFile({
        count:1,
        extension: ['.pdf'],//文件類型
        success:res=>{
          let tempFiles = res.tempFiles
          let tempFilePaths = res.tempFilePaths
          uni.uploadFile({
            url: `${that.$imgUrl}/api/common/upload`,
            filePath: tempFilePaths[0],
            header: {
              token: uni.getStorageSync('token')
            },
            name: "file",
          }).then(res => {
            let data = JSON.parse(res.data)
            if(data.code==1){
              that.file_list.push(data.data.fullurl)
            }else{
              uni.showToast({
                icon:'none',
                title: data.msg||'系統異常'
              })
            }
          })
        }
      })
    },
    uploadImages(){
      let that=this
      uni.chooseImage({
        // original 原图， compressed 压缩图，默认二者都有
        sizeType: ["original", "compressed"],
        // camera 使用相机，album 从相册中选图，默认二者都有
        sourceType: ["camera", "album"],
        count:1,
        // success是必须要写的
        success: function (res) {
          const tempFiles = res.tempFiles
          const tempFilePaths = res.tempFilePaths
          uni.uploadFile({
            url: `${that.$imgUrl}/api/common/upload`,
            filePath: tempFilePaths[0],
            header: {
              token: uni.getStorageSync('token')
            },
            name: "file",
          }).then(res => {
            let data = JSON.parse(res.data)
            if(data.code==1){
              that.upload_list.push(data.data.fullurl)
            }else{
              uni.showToast({
                icon:'none',
                title: data.msg||'系統異常'
              })
            }
          })
        }
      })
    },
    handleLX(){
      let that=this
      uni.showActionSheet({
        itemList: that.lxList,
        success: ({ tapIndex }) => {
          this.lxIndex=tapIndex
        },
        fail: (error) => {}
      })
    },
    handleSubmit(){
      if(!(this.begin&&this.end)){
        uni.showToast({
          icon:'none',
          title: '請選擇工作日期'
        })
        return false
      }
      if(!this.content){
        uni.showToast({
          icon:'none',
          title: '請填寫工作內容'
        })
        return false
      }
      if(!this.signImgUrl){
        uni.showToast({
          icon:'none',
          title: '請上傳簽名圖片'
        })
        return false
      }
      let that=this
      let params={
        report_type:parseInt(that.typeIndex+1),
        work_type:parseInt(that.lxIndex+1),
        begin:that.begin,
        end:that.end,
        content:that.content,
        work_image:that.upload_list.length>0?that.upload_list.length>1?that.upload_list.join(','):that.upload_list[0]:'',
        work_file:that.file_list.length>0?that.file_list.length>1?that.file_list.join(','):that.file_list[0]:'',
        signup_image:that.signImgUrl
      }
      this.submitForm(params)
    },
    async submitForm(params){
      let that=this
      let res = await attendanceUploadwork(params)
      if(res.code==1){
        uni.showToast({
          title: '提交成功',
          duration: 1200,
          icon:'none'
        })
        setTimeout(() => {
          uni.navigateBack()
        }, 1200);
      }else{
        uni.showToast({
          title: res.msg||'系統異常',
          duration: 2000,
          icon:'error'
        })
      }
    },
    changeCalendar(e) {
      this.begin=e[0]
      this.end=e[1]
		},
	}
};
</script>
<style lang="scss" scoped>
.leave_box{
  width: 100%;
  height: 100vh;
  overflow-y: scroll;
  background: #F4F6FA;
}
.rb-title{
  height: 25px;
  font-weight: 600;
  font-size: 18px;
  color: #333333;
  line-height: 25px;
  margin: 20px 12px 13px;
}
.rb-tabs{
  width: calc(100% - 20px);
  padding: 10px;
  background: #fff;
  border-radius: 6px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  font-weight: 500;
  font-size: 15px;
  color: #333333;
  line-height: 38px;
  margin: 0 auto;
  view{
    width: 60px;
    height: 38px;
    border-radius: 6px;
    text-align: center;
    margin-right: 10px;
  }
  view.active{
    background: #1C7AF7;
    color: #fff;
  }
}
.rb-block{
  width: calc(100% - 20px);
  padding: 10px;
  margin: 0 auto 0;
  min-height: 160px;
  background: #fff;
  border-radius: 6px;
  .reason-content{
    width: 100%;
    color: #333333;
    font-size: 14px;
    line-height: 24px;
  }
}
.rb-imgs{
  width: calc(100% - 20px);
  padding: 10px;
  margin: 10px auto 0;
  background: #fff;
  border-radius: 6px;
}
.rb-tp-p{
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
  color: #333333;
  line-height: 23px;
  img{
    display: block;
    width: 22px;
    height: 22px;
  }
}
.xc-imgs{
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
}
.xc-img{
  width: 25%;
  height: 70px;
  position: relative;
  margin-bottom: 5px;
  img.close{
    width: 14px;
    height: 14px;
    position: absolute;
    top: -7px;
    left: 63px;
  }
  img{
    width: 70px;
    height: 70px;
    border-radius: 6px;
  }
}
.xc-files{
  background: #F4F6FA;
  border-radius: 6px;
}
.xc-file{
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: calc(100% - 40px);
  padding: 10px;
  .xc-file-name{
    width: calc(100% - 55px);
    margin: 0 0 0 10px;
    height: 29px;
    font-size: 15px;
    color: #333333;
    line-height: 29px;
  }
  img{
    width: 29px;
    height: 29px;
    display: block;
  }
  img.img-point{
    width: 15px;
    height: 4px;
  }
}
.block-sign{
  width: calc(100% - 20px);
  padding: 10px;
  margin: 10px auto 20px;
  border-radius: 6px;
  background: #fff;
}
.p-sign{
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  font-size: 16px;
  color: #333333;
  line-height: 22px;
  img{
    width: 10px;
    height: 6px;
    display: block;
  }
}
.box-sign{
  width: 100%;
  height: 120px;
  padding: 20px 0 10px 0;
  p{
    color: #ccc;
    text-align: center;
    padding-top: 30px;
  }
  img{
    width: 188px;
    height: 127px;
    margin: 0 auto;
    display: block;
  }
}
.btn-submit{
  width: 335px;
  height: 44px;
  background: linear-gradient( 90deg, #016CF1 0%, #0697F6 100%);
  border-radius: 8px;
  font-weight: 500;
  font-size: 15px;
  color: #FFFFFF;
  line-height: 44px;
  text-align: center;
  margin: 0 auto 20px;
}
.rb-rq{
  width:calc(100% - 20px);
  font-weight: 500;
  font-size: 15px;
  color: #333333;
  line-height: 21px;
  padding: 10px;
  background: #fff;
  border-radius: 6px;
  margin: 0 auto;
}
.rb-lx{
  width:calc(100% - 20px);
  font-weight: 500;
  font-size: 15px;
  color: #333333;
  line-height: 21px;
  padding: 10px;
  background: #fff;
  border-radius: 6px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  img{
    width: 10px;
    height: 6px;
    display: block;
  }
}
</style>
